package com.infoauto.opc.gateway.opc.listener;

import com.infoauto.opc.gateway.messages.handler.RabbitMQListener;
import com.infoauto.opc.gateway.messages.util.GenericsUtils;
import com.infoauto.opc.gateway.model.OpcCommand;
import com.infoauto.opc.gateway.opc.factory.MqttInterfaceNodeFactory;
import com.infoauto.opc.gateway.opc.factory.OpcClientFactory;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;

//OPC服务控制指令接收监听器
public class OpcCommandListener extends RabbitMQListener {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public boolean onMessage(String s, String s1, String s2, Object o, Variant variant) {
        try
        {
            Map<String,String> coms = (LinkedHashMap)o;

            switch (coms.get("command"))
            {
                case "GATEWAY_RESTART"://重新启动网关
                    OpcClientFactory.This().restart(variant);
                    break;
                case "ANDON_SCREEN"://推送完整MQ接口数据
                    logger.info("received andon command = {} ", coms.get("content"));
                    MqttInterfaceNodeFactory.This().publishMessage(coms.get("content"));
                    break;
                default:
                    break;
            }
        }
        catch (Exception e){
            logger.error("OpcCommandListener ERROR = {}",e.getMessage());
        }

        return true;
    }

    @Override
    public Class getGenericType() {
        return GenericsUtils.getSuperClassGenericType((new OpcCommand()).getClass());
    }
}
